package com.tianqu.tms.www.assess.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.assess.actionform.AssessSerchInputForm;
import com.tianqu.tms.www.assess.viewhelper.AssessCarInfoInputViewHelper;

/**
 * 
 * 用于车辆评估信息录入中间跳转Action
 * 
 * @author Administrator
 * 
 */
public class CarAssessInputTopDisPatchAction extends TmsAction {

	private Logger logger = Logger
			.getLogger(CarAssessInputTopDisPatchAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		AssessSerchInputForm inputForm = (AssessSerchInputForm) form;

		AssessCarInfoInputViewHelper view = new AssessCarInfoInputViewHelper();

		if (uc.getAttribute(TmsConstants.VIEWHELP) != null) {
			view = (AssessCarInfoInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		}
		
		view = setViewFromForm(view, inputForm);
		String actionForward = "";
		if (inputForm.getSaveInputInfo() != null&& !"".equals(inputForm.getSaveInputInfo())) {
			actionForward = "goPrintAction";
		}
		if (inputForm.getSearchBaseInfo() != null&& !"".equals(inputForm.getSearchBaseInfo())) {
			String vin = inputForm.getVin();
			if (vin != null && !"".equals(vin)) {
				Car car = new Car();
				car = car.findCarBaseInfoByVin(vin);
				if(car == null)
				{
					setErrorsToRequest(request, TmsConstants.IA003, null);
					return mapping.getInputForward();				
				}
				if (car.getLicensePlate() == null|| "".equals(car.getLicensePlate())) {
					setErrorsToRequest(request, TmsConstants.IA003, null);
					return mapping.getInputForward();
				}
				view = setViewFromEntity(view, car);
				TransferAcceptedInfo accept = new TransferAcceptedInfo();
				accept = accept.searchLastAssessInfoByVin(vin);
				if (accept != null) {
					if (accept.getPg_price() > 0) {
						view.setLastPgPrice(StringUtil.addDH(String.valueOf(accept.getPg_price())));
					} else {
						view.setLastPgPrice("");
					}
					if (accept.getPg_time() != null&& !"".equals(accept.getPg_time())) {
						view.setLastPgTime(accept.getPg_time());
					} else {
						view.setLastPgTime("");
					}

				} else {
					view.setLastPgPrice("");
					view.setLastPgTime("");
				}
			}
			actionForward = "goInputAction";
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}

	/**
	 * 将页面信息set到viewhelp中
	 */
	private AssessCarInfoInputViewHelper setViewFromForm(
			AssessCarInfoInputViewHelper view, AssessSerchInputForm form) {

		view.setVin(form.getVin());
		view.setVendorCode(form.getVendorCode());
		view.setModelCode(form.getModelCode());
		view.setCarType(form.getCarType());
		view.setColor(form.getColor());
		view.setCarUse(form.getCarUse());
		view.setRegDate(form.getRegDate());
		view.setSendCardDate(form.getSendCardDate());
		view.setAssessPirce(form.getAssessPirce());
		view.setVendorId(form.getVendorId());
		view.setBrandId(form.getBrandId());
		view.setCarStyleId(form.getCarStyleId());
		view.setCarOwner(form.getCarOwner());
		return view;

	}

	/**
	 * 将entity信息set到view中
	 */
	private AssessCarInfoInputViewHelper setViewFromEntity(
			AssessCarInfoInputViewHelper view, Car entity) {
		view.setVin(entity.getVin());
		view.setVendorCode(entity.getVendorCode());
		view.setModelCode(entity.getModelCode());
		view.setCarType(String.valueOf(entity.getCarType()));
		view.setColor(String.valueOf(entity.getColor()));
		view.setCarUse(String.valueOf(entity.getCarUse()));
		view.setRegDate(entity.getRegDate());
		view.setSendCardDate(entity.getSendCardDate());
		view.setVendorId(entity.getVendorId());
		view.setBrandId(entity.getBrandId());
		view.setCarStyleId(entity.getCarStyleId());
		view.setCarOwner(entity.getCarOwner());

		return view;
	}

}
